package org.dregs.service.impl;

import org.dregs.pojo.Version;
import org.dregs.repository.IndexMapper;
import org.dregs.service.IndexService;
import org.dregs.utils.BuildMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;
import static org.dregs.utils.BuildMap.*;
@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    private IndexMapper indexMapper;

    public Map<String,Object> versionCompare(String md5){
        BuildMap<String, Object> buildMap = new BuildMap<>();
        Version version = indexMapper.queryByMD5(md5);
        if(null == version)
            return buildMap.put("code",1).put("msg","This version was not found").toMap();
        if(1 == version.getCurrent())
            return buildMap.put("code",0).put("msg","Success").put("data",version).toMap();
        if(null == (version = indexMapper.queryCurrentUse(version.getFileID())))
            return buildMap.put("code",1).put("msg","System Error").toMap();
        return buildMap.put("code",0).put("msg","Success").put("data",version).toMap();
    }

    @Override
    public Map<String, Object> getAllInfo() {
        return asMap(
                asTuple2("code",0),
                asTuple2("msg","Success"),
                asTuple2("data",
                        asMap(
                                asTuple2("fileList",indexMapper.queryAllFileType()),
                                asTuple2("versionList",indexMapper.queryAllVersion())
                        )
                )
        );
    }

}
